package com.youfang.sixthmedia;

import java.io.IOException;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.gzit.utils.AndroidUtils;
import com.youfang.sixthmedia.common.YoufangContext;
import com.youfang.sixthmedia.controller.HeaderController;
import com.youfang.sixthmedia.controller.LogoController;
import com.youfang.sixthmedia.controller.PhotoController;

public class SettingActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.setting_acitivity);

		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {

				case R.id.selcity:
					gotoCityActivity();
					break;

				case R.id.clearCache:
					clearCache();
					break;
				case R.id.agreement:
					gotoAgreement();
					break;
				case R.id.feedback:
					gotoFeedback();
					break;
				case R.id.about:
					gotoAbout();

					break;

				}

			}

		};

		for (int id : new int[] { R.id.selcity, R.id.clearCache,
				R.id.agreement, R.id.feedback, R.id.about }) {
			this.findViewById(id).setOnClickListener(listener);

		}

		((RadioGroup) this.findViewById(R.id.navigator_rb_container))
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {

						switch (checkedId) {
						case R.id.yesRB:
							YoufangContext.saveShowPic(true);
							break;
						case R.id.noRB:
							YoufangContext.saveShowPic(false);
							break;
						}

					}
				});

		((TextView) this.findViewById(R.id.cityName)).setText(YoufangContext
				.getCity().name);

		((RadioButton) this.findViewById(YoufangContext.showPic ? R.id.yesRB
				: R.id.noRB)).setChecked(true);

	}

	private void clearCache() {

		AndroidUtils.Dialog.confirm(this, "提示", "是否删除所缓存楼盘及房友图像?",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {

						try {
							HeaderController.Cache.clearCache();
							LogoController.Cache.clearCache();
							PhotoController.Cache.clearCache();

						} catch (IOException e) {
							e.printStackTrace();
						}

					}
				});

	}

	private void gotoAgreement() {

		Intent intent = new Intent();
		intent.setClass(this, AgreeActivity.class);
		this.startActivity(intent);

	}

	private void gotoFeedback() {

		Intent intent = new Intent();
		intent.setClass(this, FeedbackActivity.class);
		this.startActivity(intent);

	}

	private void gotoAbout() {

		Intent intent = new Intent();
		intent.setClass(this, AboutActivity.class);
		this.startActivity(intent);

	}

	private static final int CITY_REQUEST_CODE = 2;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode != Activity.RESULT_OK) {
			return;
		}

		switch (requestCode) {

		case CITY_REQUEST_CODE:
			((TextView) this.findViewById(R.id.cityName))
					.setText(YoufangContext.getCity().name);
			break;
		}

	}

	private void gotoCityActivity() {
		Intent intent = new Intent();
		intent.setClass(this, CityActivity.class);
		this.startActivityForResult(intent, CITY_REQUEST_CODE);
	}

}